
package com.scalar.chiptrack.users.actions;

import java.util.HashMap;
import java.util.NoSuchElementException;
import java.util.StringTokenizer;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.scalar.ScalarException;
import com.scalar.chiptrack.ChipTrackAction;
import com.scalar.chiptrack.operations.vendor.dao.VendorDAO;
import com.scalar.chiptrack.operations.process.dao.NewVendorLocationDAO;
import com.scalar.chiptrack.commons.Address;
import com.scalar.chiptrack.commons.SessionKeys;
import com.scalar.chiptrack.commons.Constants;
import com.scalar.chiptrack.db.DBTransaction;
import com.scalar.chiptrack.users.Functionality;
import com.scalar.chiptrack.users.SalesInfo;
import com.scalar.chiptrack.users.User;
import com.scalar.chiptrack.users.dao.UserDAO;
import com.scalar.chiptrack.users.forms.ClientInfoForm;


public class ClientInfoAction extends ChipTrackAction {


    public ActionForward execute (ActionMapping mapping,
                                  ActionForm form,
                                  HttpServletRequest request,
                                  HttpServletResponse response)
                                  throws Exception {
        HashMap info = new HashMap();
        HttpSession session = request.getSession();
        String clientaddressId = null;
        HashMap bankInfoMap = new HashMap();
        User user = ( User ) session.getAttribute ( SessionKeys.USER_KEY );
		DBTransaction dbTransaction = null;
		ClientInfoForm clientInfoForm = (ClientInfoForm) form;
        ArrayList locationList = new ArrayList();

        String action = request.getParameter ("action");
        String clientName = null;
        String address1 = null;
        String address2 = null;
		String address3 = null;
		String address4 = null;
        String city = null;
        String state = null;
        String zipCode = null;
        String country = null;
        String phoneNo = null;
        String faxNo = null;
        String email = null;
		String ccatsNumber = null;
        String bankName = null;
        String bankAddress = null;
        String bankCity = null;
        String bankState = null;
        String bankZipcode = null;
        String bankCountry = null;
        String aba = null;
        String acct = null;
        String swiftCode = null;
		int bankID = 0;
		String salesOrderPrefix = null;
		int salesOrderLength = 0;
        int jitWindow = 0;
        int shipJitWindow = 0;
        int sortProcessTime = 0;
        int assmProcessTime = 0;

        String quotationPrefix = null;
        int quotationLength = 0;

        String rmaPrefix = null;
        int rmaLength = 0;
        String psPrefix = null;
        int psLength = 0;
        String purchaseOrderNoPrefix = null;
        int purchaseOrderNoLength = 0;
        String autoReceiptNoPrefix = null;
        int autoReceiptNoLength = 0;
        String programReleasePrefix = null;
        int programReleaseLength = 0;
        String sortReleasePrefix = null;
        int sortReleaseLength = 0;
        String assmReleasePrefix = null;
        int assmReleaseLength = 0;
        String shipRequestPrefix = null;
        int shipRequestLength = 0;

        String testReleasePrefix = null;
        int testReleaseLength = 0;

        int shippingLocationId   = 0;

        String bankId = request.getParameter("bankId");
        if ( user != null )
        {
            if ( user.isActionAllowed ( Functionality.COMPANY_SETUP , User.USER_ACTION_MODIFY ) )
            {
                request.setAttribute ( "showMessage", "FALSE" );
            }
            else
            {
                request.setAttribute ( "showMessage", "TRUE" );
            }
        }
		int finalbankId = 0;
        
        if( bankId != null && bankId.trim().length() != 0 )
	    {
			   finalbankId = Integer.parseInt(bankId);
		}
        if ((session == null) || (session.getAttribute (SessionKeys.USER_KEY) == null)) {
            return (mapping.findForward("login"));
        }
		if( action == null )
		 {
				finalbankId = UserDAO.loadBankId();
				clientInfoForm.setBankNames(""+finalbankId);
				bankInfoMap = UserDAO.loadClientBankAddressDetails( dbTransaction , finalbankId );
				request.setAttribute( "BankInfo" , bankInfoMap );
                request.setAttribute("locations", NewVendorLocationDAO.loadVendorsByPSType( null, Constants.LOCATION_TYPE_FINISHED_GOODS ));
         }
        else
        if("saveClientInfo".equals(action))
        {
            SalesInfo salesInfo = new SalesInfo(); //Added by Rohan on July 22nd 2004

            clientName  = clientInfoForm.getClientName();
            address1    = clientInfoForm.getAddress1();
            address2    = clientInfoForm.getAddress2();
			address3    = clientInfoForm.getAddress3();
			address4    = clientInfoForm.getAddress4();
            city        = clientInfoForm.getCity();
            state       = clientInfoForm.getState();
            zipCode     = clientInfoForm.getZipCode();
            country     = clientInfoForm.getCountry();
            phoneNo     = clientInfoForm.getPhoneNumber();
            faxNo       = clientInfoForm.getFaxNumber();
            email       = clientInfoForm.getEmail();
			ccatsNumber = clientInfoForm.getCcatsNumber();
            bankName    = clientInfoForm.getBankName();
            jitWindow   = clientInfoForm.getJitWindow();
            shipJitWindow = clientInfoForm.getShipJitWindow();
            sortProcessTime = clientInfoForm.getSortProcessTime();
            assmProcessTime = clientInfoForm.getAssemblyProcessTime();
            shippingLocationId= clientInfoForm.getShippingLocationId();
            //Modified by Rohan July 22nd 2004
            //salesOrderPrefix = clientInfoForm.getSalesOrderPrefix();
            //salesOrderLength = clientInfoForm.getSalesOrderLength();

            // Prabhanjan (30 May 2004)
            //quotationPrefix = clientInfoForm.getQuotationPrefix();
            //quotationLength = clientInfoForm.getQuotationLength();

            salesInfo.setSalesOrderPrefix( clientInfoForm.getSalesOrderPrefix() );
            salesInfo.setSalesOrderLength( clientInfoForm.getSalesOrderLength() );
            salesInfo.setQuotationPrefix(  clientInfoForm.getQuotationPrefix() );
            salesInfo.setQuotationLength(  clientInfoForm.getQuotationLength() );
            salesInfo.setRmaPrefix(        clientInfoForm.getRmaPrefix() );
            salesInfo.setRmaLength(        clientInfoForm.getRmaLength() );
            salesInfo.setPsPrefix(         clientInfoForm.getPsPrefix() );
            salesInfo.setPsLength(         clientInfoForm.getPsLength() );
            salesInfo.setPurchaseOrderNoPrefix( clientInfoForm.getPurchaseOrderNoPrefix() );
            salesInfo.setPurchaseOrderNoLength( clientInfoForm.getPurchaseOrderNoLength() );
            salesInfo.setAutoReceiptNoPrefix( clientInfoForm.getAutoReceiptNoPrefix() );
            salesInfo.setAutoReceiptNoLength( clientInfoForm.getAutoReceiptNoLength() );
            salesInfo.setProgramReleasePrefix( clientInfoForm.getProgramReleasePrefix() );
            salesInfo.setProgramReleaseLength( clientInfoForm.getProgramReleaseLength() );
            salesInfo.setSortReleasePrefix( clientInfoForm.getSortReleasePrefix() );
            salesInfo.setSortReleaseLength( clientInfoForm.getSortReleaseLength() );
            salesInfo.setAssmReleasePrefix( clientInfoForm.getAssmReleasePrefix() );
            salesInfo.setAssmReleaseLength( clientInfoForm.getAssmReleaseLength() );
            salesInfo.setShipRequestPrefix( clientInfoForm.getShiprequestPrefix() );
            salesInfo.setShipRequestLength( clientInfoForm.getShiprequestLength() );
            salesInfo.setTestReleasePrefix( clientInfoForm.getTestReleasePrefix() );
            salesInfo.setTestReleaseLength( clientInfoForm.getTestReleaseLength() );


            //End of additions by Rohan on July 22nd 2004

            if( bankName != null )
            {
				bankName = bankName.trim();
            }
            bankAddress = clientInfoForm.getBankAddress();
            bankCity    = clientInfoForm.getBankCity();
            bankState   = clientInfoForm.getBankState();
            bankZipcode = clientInfoForm.getBankZipCode();
            bankCountry = clientInfoForm.getBankCountry();
            aba         = clientInfoForm.getAba();
            acct        = clientInfoForm.getAcct();
            swiftCode   = clientInfoForm.getSwiftCode();

            try{
                    dbTransaction = DBTransaction.getInstance();
                    //Modified by Prabhanjan (30 Muy 2004)
				    //UserDAO.saveSalesOrderInfo(dbTransaction , salesOrderPrefix , salesOrderLength );
                    //UserDAO.saveSalesOrderInfo(dbTransaction , salesOrderPrefix , salesOrderLength, quotationPrefix , quotationLength);
                    UserDAO.saveSalesOrderInfo( dbTransaction, salesInfo );
                    int addressId = UserDAO.saveClientAddressDetails(dbTransaction, clientName, address1, address2,address3, address4, city, state, zipCode, country, phoneNo, faxNo, email);
                    finalbankId = UserDAO.saveClientBankAddressDetails(dbTransaction, bankName, bankAddress, bankCity, bankState, bankZipcode, bankCountry, aba, acct, swiftCode, finalbankId );
            
                    UserDAO.saveClientInfo(dbTransaction, clientName , addressId, ccatsNumber, jitWindow, shipJitWindow,sortProcessTime,assmProcessTime, shippingLocationId );
				    bankInfoMap = UserDAO.loadClientBankAddressDetails( dbTransaction , finalbankId );
                    locationList= NewVendorLocationDAO.loadVendorsByPSType( null, Constants.LOCATION_TYPE_FINISHED_GOODS );
					request.setAttribute( "BankInfo" , bankInfoMap );
					request.setAttribute("clientInfoForm",clientInfoForm);
                    request.setAttribute("locations", locationList);
                    info.put("Client Information saved successfully",null);

              }catch(ScalarException e){e.getRootCause();}
        }

		    String bankid = new String(""+finalbankId);
			clientInfoForm.setBankId(finalbankId);
			clientInfoForm.setBankNames(bankid);
			clientaddressId =  UserDAO.getClientAddressId();
			String addressId = null;
			String clientname = null;
			String ccatsno = null;
            int jitwindow = 0;
            int shipJit   = 0;


            bankInfoMap = UserDAO.loadClientBankAddressDetails( dbTransaction , finalbankId );
			request.setAttribute( "BankInfo" , bankInfoMap );
			request.setAttribute( "AllBankNames", UserDAO.getAllBankNames());
            request.setAttribute("locations", NewVendorLocationDAO.loadVendorsByPSType( null, Constants.LOCATION_TYPE_FINISHED_GOODS ));
            int finalAddressId = 0;
			if(clientaddressId != null)
			{
				StringTokenizer st = new StringTokenizer( clientaddressId, "@" );
				try
				{
				    addressId   = st.nextToken();
				    clientname  = st.nextToken();
					ccatsno     = st.nextToken();
                    jitwindow   = Integer.parseInt( st.nextToken() );
                    shipJit     = Integer.parseInt( st.nextToken() );
                    sortProcessTime = Integer.parseInt( st.nextToken() );
                    assmProcessTime = Integer.parseInt( st.nextToken() );
                    shippingLocationId=Integer.parseInt( st.nextToken() );
                }catch(NoSuchElementException e)
				{
				     //clientname  = "";
				}
			}
			if(addressId  != null)
			{
			   finalAddressId = Integer.parseInt(addressId);
			}
			Address address = UserDAO.loadClientAddressDetails( dbTransaction , finalAddressId );
            if(clientName == null)
            {
                clientName = "";
            }
            SalesInfo salesInfo = UserDAO.getSalesOrderInfo( dbTransaction );

            String salesPrefix = null;
            int salesNoLength = 0;

            if(salesInfo == null)
            {
				salesPrefix             = "";
				salesNoLength           = 0;
                quotationPrefix         = "";
                quotationLength         = 0;
                rmaPrefix               = "";
                rmaLength               = 0;
                psPrefix                = "";
                psLength                = 0;
                purchaseOrderNoPrefix   = "";
                purchaseOrderNoLength   = 0;
                autoReceiptNoPrefix     = "";
                autoReceiptNoLength     = 0;
                programReleasePrefix    = "";
                programReleaseLength    = 0;
                sortReleasePrefix       = "";
                sortReleaseLength       = 0;
                assmReleasePrefix       = "";
                assmReleaseLength       = 0;
                shipRequestPrefix       = "";
                shipRequestLength       = 0;
                testReleasePrefix       = "";
                testReleaseLength       = 0;
            }
            else
            {
                salesPrefix             = salesInfo.getSalesOrderPrefix();
                salesNoLength           = salesInfo.getSalesOrderLength();
                quotationPrefix         = salesInfo.getQuotationPrefix();
                quotationLength         = salesInfo.getQuotationLength();
                rmaPrefix               = salesInfo.getRmaPrefix();
                rmaLength               = salesInfo.getRmaLength();
                psPrefix                = salesInfo.getPsPrefix();
                psLength                = salesInfo.getPsLength();
                purchaseOrderNoPrefix   = salesInfo.getPurchaseOrderNoPrefix();
                purchaseOrderNoLength   = salesInfo.getPurchaseOrderNoLength();
                autoReceiptNoPrefix     = salesInfo.getAutoReceiptNoPrefix() ;
                autoReceiptNoLength     = salesInfo.getAutoReceiptNoLength() ;
                programReleasePrefix    = salesInfo.getProgramReleasePrefix() ;
                programReleaseLength    = salesInfo.getProgramReleaseLength() ;
                sortReleasePrefix       = salesInfo.getSortReleasePrefix() ;
                sortReleaseLength       = salesInfo.getSortReleaseLength() ;
                assmReleasePrefix       = salesInfo.getAssmReleasePrefix() ;
                assmReleaseLength       = salesInfo.getAssmReleaseLength() ;
                shipRequestPrefix       = salesInfo.getShipRequestPrefix();
                shipRequestLength        = salesInfo.getShipRequestLength();

                testReleasePrefix       = salesInfo.getTestReleasePrefix() ;
                testReleaseLength       = salesInfo.getTestReleaseLength() ;

            }


            clientInfoForm.setClientName(clientname);
            clientInfoForm.setAddress1(address.getAddress1());
            clientInfoForm.setAddress2(address.getAddress2());
			clientInfoForm.setAddress3(address.getAddress3());
			clientInfoForm.setAddress4(address.getAddress4());
            clientInfoForm.setCity(address.getCity());
            clientInfoForm.setState(address.getState());
            clientInfoForm.setZipCode(address.getZipCode());
            clientInfoForm.setCountry(address.getCountry());
            clientInfoForm.setPhoneNumber(address.getPhoneNumber());
            clientInfoForm.setFaxNumber(address.getFaxNumber());
            clientInfoForm.setEmail(address.getEmail());
			clientInfoForm.setCcatsNumber(ccatsno);
            clientInfoForm.setJitWindow(jitwindow);
            clientInfoForm.setSortProcessTime( sortProcessTime );
            clientInfoForm.setAssemblyProcessTime( assmProcessTime );
			clientInfoForm.setSalesOrderPrefix(salesPrefix);
			clientInfoForm.setSalesOrderLength(salesNoLength);
            clientInfoForm.setQuotationPrefix(quotationPrefix);
            clientInfoForm.setQuotationLength(quotationLength);
            clientInfoForm.setRmaPrefix(rmaPrefix);
            clientInfoForm.setRmaLength(rmaLength);
            clientInfoForm.setPsPrefix(psPrefix);
            clientInfoForm.setPsLength(psLength);
            clientInfoForm.setShipJitWindow(shipJit);
            clientInfoForm.setPurchaseOrderNoPrefix( purchaseOrderNoPrefix );
            clientInfoForm.setPurchaseOrderNoLength( purchaseOrderNoLength );
            clientInfoForm.setAutoReceiptNoPrefix( autoReceiptNoPrefix );
            clientInfoForm.setAutoReceiptNoLength( autoReceiptNoLength );
            clientInfoForm.setProgramReleasePrefix( programReleasePrefix );
            clientInfoForm.setProgramReleaseLength( programReleaseLength );
            clientInfoForm.setSortReleasePrefix( sortReleasePrefix );
            clientInfoForm.setSortReleaseLength( sortReleaseLength );
            clientInfoForm.setAssmReleasePrefix( assmReleasePrefix );
            clientInfoForm.setAssmReleaseLength( assmReleaseLength );
            clientInfoForm.setShiprequestPrefix( shipRequestPrefix );
            clientInfoForm.setShiprequestLength( shipRequestLength );

            clientInfoForm.setTestReleasePrefix( testReleasePrefix );
            clientInfoForm.setTestReleaseLength( testReleaseLength );
            clientInfoForm.setShippingLocationId(shippingLocationId);

            request.setAttribute("clientInfoForm",clientInfoForm);
            if(!info.isEmpty())
            request.setAttribute(SessionKeys.INFO_MESSAGES,info);

            return mapping.findForward("success");
    }

}